package android.TaskApp;


import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class ViewTasksActivity extends ListActivity {

	private Button addButton;
	private TaskListAdapter adapter;
	private TaskManagerApplication app;
	private Button removeButton;
	private ArrayList<Task> list = new ArrayList<Task>();
    private Button refreshButton;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_mail);
        
        setUpViews();
        app = (TaskManagerApplication)getApplication();
        
        list = new ArrayList<Task>();
		
        SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
        int userid=prefs.getInt("userid", 0);
        UserTaskListResponse response= WebServiceHelper.getUserTaskLists(userid); 
      
        if(response.getStatusCode()==1)
        {
        	UserTaskListVO[] tasks= response.getUsertasklists();
         	
         	for(int i=0;i<tasks.length;i++)
         	{
         		Task t = new Task(tasks[i].getTasklistid(),tasks[i].getTasklistname());
         	   list.add(t);
         	   Log.d("Task List names",tasks[i].getTasklistname());
         	}   
         	 
         	}
        
        else
        {
        	Toast.makeText(ViewTasksActivity.this, "No Tasks found", Toast.LENGTH_LONG).show();
        }
      
        adapter = new TaskListAdapter(this, list);
        setListAdapter(adapter);
    }
	
	@Override
	protected void onResume() {
		super.onResume();
		
		list = new ArrayList<Task>();
		
       SharedPreferences prefs= PreferenceManager.getDefaultSharedPreferences(this);
       int userid=prefs.getInt("userid", 0);
       UserTaskListResponse response= WebServiceHelper.getUserTaskLists(userid); 
      if(response.getStatusCode()>0)
       {
    	   UserTaskListVO[] tasks = response.getUsertasklists();
       	
       	for(int i=0;i<tasks.length;i++)
       	{
       	   Task t = new Task(tasks[i].getTasklistid(),tasks[i].getTasklistname());
       	   list.add(t);
       	   Log.d("Task List names",tasks[i].getTasklistname());
       	}

        adapter = new TaskListAdapter(this, list);
        setListAdapter(adapter);
        
        if(response.getStatusCode()<2)
        {
        	
        	setListAdapter(adapter);
        	//adapter.forceReload();
        }
		
       	}
       
       else
       {
    	   adapter.forceReload();
    	   Toast.makeText(ViewTasksActivity.this, "No Tasks found", Toast.LENGTH_LONG).show();
            
       }
       
       adapter.forceReload();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		adapter.toggleTaskCompleteAtPosition(position);
	}
	
	protected void removeCompletedTasks() {

		adapter.removeCompletedTasks();
	}
	
	private void setUpViews() {
		addButton = (Button)findViewById(R.id.add_button);
		removeButton = (Button)findViewById(R.id.remove_button);
		refreshButton =(Button)findViewById(R.id.refresh);
		
		
		refreshButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
			/*	Intent intent = new Intent(ViewTasksActivity.this, ViewTasksActivity.class);
				startActivity(intent);
				*/
				onResume();
			}
		});
		
		
		addButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				Intent intent = new Intent(ViewTasksActivity.this, AddTaskActivity.class);
				startActivity(intent);
			}
		});
		removeButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				removeCompletedTasks();
			}
		});

	}

}